﻿using System;
using FluentAssertions;
using Grundy.Common;
using Grundy.Entity;
using Grundy.Message;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Grundy.Licensing.Server.Tests
{
    [TestClass]
    public class FeatureIdentificationKeyMatchScoreEvaluatorTests
    {
        private FeatureIdentificationKeyMatchScoreEvaluator evaluator;

        [TestInitialize]
        public void Setup()
        {
            evaluator = new FeatureIdentificationKeyMatchScoreEvaluator();
        }

        [TestMethod]
        public void Should_return_0_for_null_fik()
        {
            var fik1 = FeatureIdentificationKey.Null;
            var fik2 = FeatureIdentificationKey.Null;
            evaluator.GetMatchScore(fik1, fik2).Should().Be(0);
        }

        [TestMethod]
        public void Should_return_less_than_0_for_none_matching()
        {
            var fik1 = new FeatureIdentificationKey{LicenseEntitlementId = Guid.NewGuid()};
            var fik2 = new FeatureIdentificationKey{LicenseEntitlementId = Guid.NewGuid()};
            evaluator.GetMatchScore(fik1, fik2).Should().BeLessOrEqualTo(0);
        }

        [TestMethod]
        public void Should_return_1_for_feature_matching_but_something_else_a_is_not_default_and_b_is_default()
        {
            var featureId = Guid.NewGuid();
            var fik1 = new FeatureIdentificationKey { FeatureId = featureId, LicenseEntitlementId = Guid.NewGuid()};
            var fik2 = new FeatureIdentificationKey { FeatureId = featureId };
            evaluator.GetMatchScore(fik1, fik2).Should().Be(1);

            fik1 = new FeatureIdentificationKey { FeatureName = "F1", LicenseEntitlementId = Guid.NewGuid() };
            fik2 = new FeatureIdentificationKey{FeatureName = "F1"};
            evaluator.GetMatchScore(fik1, fik2).Should().Be(1);
        }

        [TestMethod]
        public void Should_return_less_than_0_for_feature_matching_but_something_else_different_and_not_default()
        {
            var featureId = Guid.NewGuid();
            
            var fik1 = new FeatureIdentificationKey { FeatureName = "F1", FeatureId = Guid.NewGuid(), LicenseEntitlementId = Guid.NewGuid() };
            var fik2 = new FeatureIdentificationKey { FeatureName = "F1", FeatureId = Guid.NewGuid() };
            evaluator.GetMatchScore(fik1, fik2).Should().BeNegative();

            fik1 = new FeatureIdentificationKey { FeatureName = "F1A", FeatureId = featureId, LicenseEntitlementId = Guid.NewGuid() };
            fik2 = new FeatureIdentificationKey { FeatureName = "F1B", FeatureId = featureId };
            evaluator.GetMatchScore(fik1, fik2).Should().BeNegative();
        }

        [TestMethod]
        public void Should_return_greater_than_0_for_feature_matching()
        {
            var featureId = Guid.NewGuid();
            var fik1 = new FeatureIdentificationKey { FeatureId = featureId };
            var fik2 = new FeatureIdentificationKey { FeatureId = featureId };
            evaluator.GetMatchScore(fik1, fik2).Should().BeGreaterThan(0);

            fik1 = new FeatureIdentificationKey { FeatureName = "F1" };
            fik2 = new FeatureIdentificationKey { FeatureName = "F1" };
            evaluator.GetMatchScore(fik1, fik2).Should().BeGreaterThan(0);

            fik1 = new FeatureIdentificationKey { FeatureName = "F1", FeatureId = featureId };
            fik2 = new FeatureIdentificationKey { FeatureName = "F1", FeatureId = featureId };
            evaluator.GetMatchScore(fik1, fik2).Should().BeGreaterThan(0);
        }

        [TestMethod]
        public void Should_return_2()
        {
            var id1 = Guid.NewGuid();
            var id2 = Guid.NewGuid();
            var fik1 = new FeatureIdentificationKey { FeatureId = id1, LicenseEntitlementId = id2};
            var fik2 = new FeatureIdentificationKey { FeatureId = id1, LicenseEntitlementId = id2};
            evaluator.GetMatchScore(fik1, fik2).Should().Be(2);

            fik1 = new FeatureIdentificationKey { FeatureName = "F1", ProductId = id1 };
            fik2 = new FeatureIdentificationKey { FeatureName = "F1", ProductId = id1};
            evaluator.GetMatchScore(fik1, fik2).Should().Be(2);

            fik1 = new FeatureIdentificationKey { FeatureName = "F1", ProductName = "P1" };
            fik2 = new FeatureIdentificationKey { FeatureName = "F1", ProductName = "P1"};
            evaluator.GetMatchScore(fik1, fik2).Should().Be(2);

            fik1 = new FeatureIdentificationKey { FeatureName = "F1", LicenseName = "L1" };
            fik2 = new FeatureIdentificationKey { FeatureName = "F1", LicenseName = "L1" };
            evaluator.GetMatchScore(fik1, fik2).Should().Be(2);
        }
    }
}